/**
 * 
 */
package org.pump.client.codec;

import java.util.Arrays;

import org.apache.mina.core.buffer.IoBuffer;
import org.pump.client.type.LengthCodedBinary;
import org.pump.client.type.LengthCodedString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author liudong03
 *
 */
public class OkPack extends ResultPack{
	private static final Logger mylog = LoggerFactory.getLogger(LengthCodedBinary.class);
	LengthCodedBinary affected_rows;
	LengthCodedBinary insert_id;
	int server_status;
	int warning_count;
	LengthCodedString message;
	
	public static OkPack readBuffer(IoBuffer in,int len)
	{
		if(in.remaining() < len )
		{
			return null;
		}
		OkPack okpack = new OkPack();
		okpack.affected_rows = LengthCodedBinary.readBuffer(in);
		okpack.len += okpack.affected_rows.getLen();
		okpack.insert_id = LengthCodedBinary.readBuffer(in);
		okpack.len += okpack.insert_id.getLen();
		okpack.server_status = in.getUnsignedShort();
		okpack.len += 2;
		okpack.warning_count = in.getUnsignedShort();
		okpack.len += 2;
		
		int msglen = len - okpack.len;
		if(msglen > 0)
		{
			okpack.message = LengthCodedString.readBuffer(in);
			if(okpack.message.getLen() != msglen)
			{
				mylog.info("read message fail");
				return null;
			}
			okpack.len = len;
		}
		
		return okpack;
	}
	/**
	 * 
	 */
	public OkPack() {
		// TODO Auto-generated constructor stub
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "OkPack [field_count=" + this.resulttype + ", affected_rows="
				+ affected_rows + ", insert_id=" + insert_id
				+ ", server_status=" + this.server_status
				+ ", warning_count=" + this.warning_count
				+ ", message=" + message + "]";
	}
	
	

}
